package com.newlink.building.activity

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.common_base.constant.Base_RouterPath
import com.newlink.building.common_base.constant.NL_Key
import com.newlink.building.common_base.utils.logDebug
import com.newlink.building.databinding.ActivityNotifyBinding
import com.newlink.building.manager.Module_Phone_VoipController
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationHelper
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationInvoker
import org.linphone.core.Call

/**
 * @Author: Jake
 * @Date: 2023-12-11
 * @Description:
 */
@Route(path = Base_RouterPath.PATH_NOTIFY)
class NL_NotifyActivityNL : NL_BaseActivity() {
//    override fun attachLayoutRes(): Int = R.layout.activity_notify

    private lateinit var mBinding: ActivityNotifyBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityNotifyBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
        // 通知页面初始化时的混淆
        Module_Obfuscation_ObfuscationHelper.onMethodEnter("NotifyActivity", "initData")
        Module_Obfuscation_ObfuscationInvoker.invokeRandom(System.currentTimeMillis())

        logDebug<NL_NotifyActivityNL>("(jake) ==> NotifyActivity")
        val currentCall: Call? = Module_Phone_VoipController.get().coreContext.core.currentCall
        if (currentCall != null) {
            // 处理通话状态时的混淆
            Module_Obfuscation_ObfuscationInvoker.invokeSpecific(16, currentCall.state.name)

            logDebug<NL_NotifyActivityNL>("(jake) ==> call state is ${currentCall.state}")
            if (currentCall.state == Call.State.Connected || currentCall.state == Call.State.StreamsRunning) {
                val routerPath =
                    if (currentCall.params.getCustomHeader(NL_Key.CALL_INFO) == NL_Key.EXTRA_MONITOR) {
                        Base_RouterPath.PATH_MONITOR
                    } else {
                        Base_RouterPath.PATH_CALL
                    }
                ARouter.getInstance().build(routerPath).navigation()
            } else if (currentCall.state == Call.State.OutgoingRinging) {
                ARouter.getInstance().build(Base_RouterPath.PATH_OUTGOINGCALL).navigation()
            } else if (currentCall.state == Call.State.IncomingReceived) {
                ARouter.getInstance().build(Base_RouterPath.PATH_INCOMINGCALL).navigation()
            }
        } else {
            var tabIndex = intent.getIntExtra(NL_Key.TAB_INDEX, -1)
            var subTabIndex = intent.getIntExtra(NL_Key.SUB_TAB_INDEX, -1)
            logDebug<NL_NotifyActivityNL>("(jake) ==> tabIndex $tabIndex subTabIndex $subTabIndex")
            // 跳转到 记录->呼叫记录
            if (tabIndex != -1 && subTabIndex != -1) {
                ARouter.getInstance().build(Base_RouterPath.PATH_HOME)
                    .withInt(NL_Key.TAB_INDEX, tabIndex).withInt(NL_Key.SUB_TAB_INDEX, subTabIndex)
                    .navigation()
            } else {
                ARouter.getInstance().build(Base_RouterPath.PATH_HOME).navigation()
            }
        }

        finish()
    }

    override fun initView() {
    }
}
